﻿using System.Collections.Generic;
using System.Linq;
using CcrFlows.Internal.Stages;
using NUnit.Framework;

namespace Test.CcrFlows
{
    [TestFixture]
    public class testStageBase
    {
        [Test]
        public void Get_transitive_hull()
        {
            IEnumerable<StageBase> th;

            StageBase s = new SeqStage<int, int>((i, pi)=>{});
            th = s.TransitiveHull;
            Assert.AreEqual(1, th.Count());
            Assert.AreEqual(s, th.First());

            Assert.AreEqual(0, s.Next.Where(x=>x!=null).Count());

            StageBase t = new SeqStage<int, string>((i, pi) => { });
            s.ContinueWith(0, t);
            Assert.AreEqual(1, s.Next.Where(x => x != null).Count());

            th = s.TransitiveHull;
            Assert.AreEqual(1, th.Count());
            Assert.AreEqual(t, th.First());

            StageBase f = new ForkStage<string, int, string>(null);
            t.ContinueWith(0, f);
            th = s.TransitiveHull;
            Assert.AreEqual(1, th.Count());
            Assert.AreEqual(f, th.First());

            StageBase b0 = new SeqStage<int, bool>((i, pi) => { });
            f.ContinueWith(0, b0);
            th = s.TransitiveHull;
            Assert.AreEqual(1, th.Count());
            Assert.AreEqual(b0, th.First());

            StageBase b1 = new SeqStage<string, double>((i, pi) => { });
            f.ContinueWith(1, b1);
            th = s.TransitiveHull;
            Assert.AreEqual(2, th.Count());
            Assert.AreEqual(b0, th.First());
            Assert.AreEqual(b1, th.Last());

            StageBase j = new JoinStage<bool, double, string>(null);
            b0.ContinueWith(0, j, 0);
            b1.ContinueWith(0, j, 1);
            th = s.TransitiveHull;
            Assert.AreEqual(1, th.Count());
            Assert.AreEqual(j, th.First());
        }
    }
}
